package com.moon.admin.sys.controller;

import java.util.List;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.google.common.collect.Lists;
import com.moon.admin.framework.domain.Kv;
import com.moon.admin.framework.domain.MoonPage;
import com.moon.admin.framework.mybatisplus.query.Condition;
import com.moon.admin.framework.secure.annotation.FormRepeat;
import com.moon.admin.framework.secure.annotation.PreAuth;
import com.moon.admin.framework.springmvc.domain.R;
import com.moon.admin.framework.syslog.annotation.SysLog;
import com.moon.admin.framework.util.ToolUtils;
import com.moon.admin.sys.domain.PostListVO;
import com.moon.admin.sys.domain.query.PostQuery;
import com.moon.admin.sys.entity.SysDeptEntity;
import com.moon.admin.sys.entity.SysPostEntity;
import com.moon.admin.sys.service.SysDeptService;
import com.moon.admin.sys.service.SysPostService;

import io.swagger.annotations.Api;

/**
 * 岗位 信息操作处理
 * 
 * @author ninglong
 */
@RestController
@RequestMapping("/sysPost")
@Api(value="岗位管理",tags="岗位管理")
public class SysPostController {
	
	@Autowired
	private SysPostService postService;
	@Autowired
	private SysDeptService deptService;
	
	@PostMapping(value = "list")
	public R<MoonPage<PostListVO>> list(@RequestBody PostQuery query) {
		LambdaQueryWrapper<SysPostEntity> wrappers = Condition.getQueryWrapper(new SysPostEntity(), query);
		if(!ToolUtils.isNull(query.getDeptId())) {
			List<SysDeptEntity> depts = deptService.getDeptAllChildrenIdById(query.getDeptId());
			List<Integer> ids = depts.stream().map(entity->entity.getId()).collect(Collectors.toList());
			if(depts.size()>0)wrappers.in(SysPostEntity::getDeptId, ids);
		}
		wrappers.orderByDesc(SysPostEntity::getUpdateTime);
		IPage<SysPostEntity> page = postService.page(Condition.getPage(query), wrappers);
		List<PostListVO> records = Lists.newArrayList();
		page.getRecords().forEach(entity->{
			records.add(PostListVO.wrappers(entity));
		});
		MoonPage<PostListVO> data = new MoonPage<PostListVO>(records,page.getTotal(),page.getSize(),page.getCurrent());
		return R.success(data);
	}
	
	@PostMapping(value = "save")
	@PreAuth(value={"sys_post_add","sys_post_edit"})
	@SysLog(mod="岗位管理",fun="保存人员岗位")
	@FormRepeat
	public R<String> save(@RequestBody SysPostEntity postEntity){
		postService.saveOrUpdate(postEntity);
		return R.success();
	}
	
	@GetMapping(value = "removeByIds")
	@PreAuth(value="sys_post_remove")
	@SysLog(mod="岗位管理",fun="删除岗位")
	public R<Void> removeByIds(String ids) {
		if(ToolUtils.isBlank(ids)) {
			return R.fail("请选择要删除的岗位");
		}
		return R.status(postService.removeByIds(ToolUtils.toIntList(ids)));
	}
	
	/**
	 * 获取所有岗位，用于下拉
	 */
	@GetMapping("getList")
	public R<List<Kv>> getList(){
		List<Kv> list = postService.list().stream().map(entity->
			Kv.builder().label(entity.getPostName()).value(String.valueOf(entity.getId())).build()
		).collect(Collectors.toList());
		return R.success(list);
	}
}